package com.atwisdom.star.core.dsl.modelClass.hier;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.core.dao.pojo.modelClass.hier.HierInstance;
import com.atwisdom.star.core.dao.pojo.modelClass.hier.HierModel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

public interface HierInstanceDslService {
    /**
     * 添加
     * @param hier 类名
     * @return 返回模型记录
     */
    List<HierInstance> add(JSONObject hier);

    /**
     * 删除
     * @param model
     * @param className
     * @return
     */
    boolean deleteByClassName(String model, String className);

    /**
     * 根据linkModel和linkClassName删除实例
     * @param linkModel 关联模型
     * @param linkClassName 关联模型名称
     * @return
     */
    boolean deleteByLinkClassName(String linkModel, String linkClassName);


    List<HierInstance> add(String containerModel, String className, Long containerId);

    List<HierInstance> addChild(String model, String className, Long containerId, String parentNodeId, List<JSONObject> dataJsonList);

    List<HierInstance> add(Long containerId, List<HierModel> hierModelList);

    HierInstance add(Long containerId, HierModel hierModel);

    List<HierInstance> fetchByHierModel(HierModel firstModel);

    List<HierInstance> fetchByModelAndClassName(String model, String className);

    HierInstance add(HierInstance hierInstance);

    List<HierInstance> addBatch(List<HierInstance> hierInstanceList);

    HierInstance addByParentHierInstance(HierInstance parentHierInstance, Long linkId);

    HierModel fetchFirstHierModelWithProperty(String model, String className, String parentNodeId);

    HierModel fetchFirstHierModelWithProperty(HierInstance hierInstance);

    /**
     * 更新是否在workCenter状态
     * @param id
     * @return
     */
    boolean updateInnerStatus(Long id);

    /**
     * 更新是否在workCenter状态
     * @param idList
     * @return
     */
    boolean updateInnerStatus(List<Long> idList);

    List<HierInstance> updateByContainerId(String model, String className, Long containerId, JSONObject updateHierJson);

    void updateByModel(List<HierModel> oldHierModelList, List<HierModel> newHierModelList);

    HierInstance updateById(HierInstance hierInstance);
    /**
     * 根据Id更新记录有null值
     * @param hierInstance
     * @return
     */
    HierInstance updateNullById(HierInstance hierInstance);

    boolean updateBatchById(List<HierInstance> hierInstanceList);

    /**
     * 查询
     * @param model
     * @param className
     * @param condition
     * @param orderBy
     * @return
     */
    List<HierInstance> query(String model, String className, String condition, String orderBy) ;
    List<HierInstance> query(String model, String className, String condition) ;
    /**
     * 根据 主model和主ClassName，关联model和关联ClassName 查询关联的hier数据，并不进行value处理转换
     * @param containerModel 主模型
     * @param className 主类名
     * @param LinkModel 关联模型
     * @param LinkClassName 关联类名
     * @return 关联的hier数据
     */
    List<HierInstance> queryLink(String containerModel, String className,String parentId, String LinkModel, String LinkClassName, Long linkId);
    /**
     * 根据 主model和主ClassName，关联model和关联ClassName和关联Link 查询关联的hier数据，并不进行value处理转换
     * @param containerModel 主模型
     * @param LinkModel 关联模型
     * @param linkId 关联实例id
     * @return 关联的hier数据
     */
    List<HierInstance> queryLink(String containerModel, String LinkModel, Long linkId);
    /**
     * 查询数量
     * @param model
     * @param className
     * @param condition
     * @return
     */
    long count(String model, String className, String condition) ;

    long count(String model, String className, String condition,String parentId);

    /**
     * 返回容器实例hier
     * @param containerId 容器id
     * @return
     */
    List<HierInstance> queryByContainerId(String model, String className, Long containerId) ;

    List<HierInstance> queryByContainerId(String model, Long containerId);

    List<HierInstance> queryByContainerId(String model, Long containerId, String className, String condition);

    List<HierInstance> queryByContainerId(String model, Long containerId, String className, String condition, boolean needRootFlag);

    Page<HierInstance> pageByContainerId(String model, Long containerId, String className,
                                         String condition, boolean needRootFlag, long current, long size, String orderBy);

    JSONObject queryById(Long id);

    HierInstance queryHierInsById(Long id);

    List<HierInstance> queryChildByContainerId(String model, String className, Long containerId, String parentId, String condition);

    List<HierInstance> convertValuesByProperty(List<HierInstance> list);

    JSONObject convertValuesByProperty(HierInstance hierInstance);

    List<HierInstance> convertValuesByProperty(List<HierInstance> hierInstanceList, HierModel hierModel);

    HierInstance convertValuesByProperty(HierInstance hierInstance, HierModel hierModel);

    List<HierInstance> queryNextByNodeId(String model, String className, String nodeId);

    void updateStatus(Long id, String code);

    Map<Long, String> fetchCondition(List<HierInstance> nextInstanceList);

    boolean deleteByIdList(List<Long> idList);

    boolean deleteById(Long id);

    boolean deleteByClassNameList(String model, List<String> classNameList);

    /**
     * 根据linkModel和linkClassNameList数组删除实例
     * @param linkModel 关联模型
     * @param linkClassNameList 关联模型名称数组
     * @return
     */
    boolean deleteByLinkClassNameList(String linkModel, List<String> linkClassNameList);

    boolean deleteByContainerIdList(String model, List<Long> containerIdList);

    boolean deleteByContainerId(String model, Long containerId);

    /**
     * 根据linkModel和linkIdList数组删除实例
     * @param linkModel 关联模型
     * @param linkIdList 关联实例id数组
     * @return
     */
    boolean deleteByLinkIdList(String linkModel, List<Long> linkIdList);

    /**
     * 根据linkModel和linkId删除实例
     * @param linkModel 关联模型
     * @param linkId 关联实例id
     * @return
     */
    boolean deleteByLinkId(String linkModel, Long linkId);

    boolean deleteByParentId(String model, Long containerId, String parentNodeId);

    HierInstance fetchByLinkClassName(String model, Long containerId, String linkClassName);

    <T> T fetchValue(HierInstance instance, String nodeName, String path);

    HierInstance fetchByLinkId(String model, Long containerId, Long linkId);

    HierInstance fetchByNodeId(String model, String className, Long containerId, String nodeId);

    HierInstance fetchByNodeId(String model, Long containerId, String nodeId);

    List<HierInstance> fetchByParentNodeId(String model, Long containerId, String parentNodeId);

    Map<Object, List<HierInstance>> fetchByParentNodeIdAndGroupBy(String model, Long containerId, String parentNodeId, String groupByKey);

    List<HierInstance> fetchByName(String model, String className, Long containerId, String name);

    List<HierInstance> fetchByParentId(String model, String className, Long containerId, String parentId);

    List<HierInstance> fetchAllNode(String model, Long containerId);

    List<HierInstance> fetchAllNode(String model, String className, Long containerId);

    List<HierInstance> fetchAllNode(String model, String className, Long containerId, Predicate<HierInstance> predicate);

    List<HierInstance> fetchAllNode(String model, Long containerId, Predicate<HierInstance> predicate);

    List<HierInstance> filterNodeList(List<HierInstance> hierInstanceList, Predicate<HierInstance> predicate);

    HierInstance filterNode(List<HierInstance> hierInstanceList, Predicate<HierInstance> predicate);

    <T> T fetchValue(HierInstance instance, String path);

    Map<String, Object> fetchLink(HierInstance instance);

    String fetchCondition(Long hierInstanceId);

    Boolean fetchCreateInsFlag(Long hierInstanceId);

    HierInstance getById(Long hierInstanceId);

    List<HierInstance> fetchToReadyNodeList(HierInstance currentInstance, Predicate<String> predicateCondition);

    void updateNodeListToReady(HierInstance hierInstance, Predicate<String> predicateCondition);

    HierInstance updateStatus(String containerModel, Long containerId, String nodeId, String statusCode);
    boolean updateOccupyStatus(String containerModel, Long containerId, List<String> nodeIdList, String statusCode);

    boolean updateClassName(String model, String className, String oldClassName);

    HierInstance saveOrUpdate(HierInstance hierInstance);
    List<HierInstance> saveOrUpdateBatch(List<HierInstance> hierInstanceList);

    @Transactional(rollbackFor = Exception.class)
    void addBatchChildBySameParent(List<JSONObject> hierInstanceJsonList);

    void addChild(List<JSONObject> hierInstanceJsonList);

    HierInstance addChild(JSONObject hierInstanceJson);

    HierInstance saveChild(Long containerId, String parentLinkModel,
                           String parentLinkClassName, Long linkId);

    List<HierInstance> saveChildBatch(Long containerId, String parentLinkModel,
                                      String parentLinkClassName, List<Long> linkIdList);

    List<HierInstance>  addNodeAndLinkInstanceToListContainer(JSONObject data);

    boolean deleteNodeByLink(JSONObject data);

    List<HierInstance> updateByIds(JSONObject data);

    HierInstance buildNewInstance(HierModel hierModel, JSONObject jsonObject);

    JSONObject convertToJson(HierInstance instance);

    List<JSONObject> convertToJson(List<HierInstance> list);

    // HierInstance saveByHierModel(HierModel hierModel, JSONObject dataJson);

}
